package gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import entities.Appointment;
import exceptions.IllegalAppointmentException;
import exceptions.KeinFreierTermin;
import fachlogik.Administration;

public class RescheduleWindow2 extends JFrame {

private JPanel               contentPane;
private JTextField           tf_von;
private JTextField           tf_bis;
private SimpleDateFormat     sf;
private Appointment          appointmentToReschedule;
private Administration       instance;
private DefaultComboBoxModel dcm;
private AppointmentDialog    appdia;

/**
 * Launch the application.
 */
// public static void main(String[] args) {
// EventQueue.invokeLater(new Runnable() {
// public void run() {
// try {
// RescheduleWindow frame = new RescheduleWindow();
// frame.setVisible(true);
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
// });
// }

/**
 * Create the frame.
 */
public RescheduleWindow2(Administration admin, Appointment a, final AppointmentDialog ad) {
    this.appdia = ad;
    addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent arg0) {
            appdia.setVisible(true);
        }
    });
    instance = admin;
    appointmentToReschedule = a;
    sf = new SimpleDateFormat("dd.MM.yyyy");
    setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    setBounds(100, 100, 449, 173);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);
	setTitle("Reschedule");

    JPanel panel = new JPanel();
    contentPane.add(panel, BorderLayout.CENTER);
    panel.setLayout(null);

    JLabel lblReschedule = new JLabel("Reschedule");
    lblReschedule.setBounds(10, 11, 95, 14);
    panel.add(lblReschedule);

    JLabel lblVon = new JLabel("From:");
    lblVon.setBounds(20, 36, 46, 14);
    panel.add(lblVon);

    tf_von = new JTextField();
    tf_von.setBounds(60, 36, 104, 20);
    panel.add(tf_von);
    tf_von.setColumns(10);

    tf_von.setText(sf.format(new Date()));

    JLabel lblBis = new JLabel("To:");
    lblBis.setBounds(184, 36, 46, 14);
    panel.add(lblBis);

    tf_bis = new JTextField();
    tf_bis.setBounds(236, 36, 112, 20);
    panel.add(tf_bis);
    tf_bis.setColumns(10);

    JButton btnReschedule = new JButton("Reschedule");
    btnReschedule.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                Date von = sf.parse(tf_von.getText());
                Date bis = sf.parse(tf_bis.getText());
                Date today = new Date();
                if (von.after(today) && bis.after(today)) {
                    instance.setRescheduleErlaubtVon(von);
                    instance.setRescheduleErlaubtBis(bis);
                    instance.reschedule(von, bis, appointmentToReschedule);
                }
            } catch (ParseException e1) {
                JOptionPane.showMessageDialog(null, "Please enter the date thusly: dd.MM.yyyy");
            } catch (KeinFreierTermin e1) {
                JOptionPane.showMessageDialog(null, e1.getMessage());
            } catch (IllegalAppointmentException e1) {
                JOptionPane.showMessageDialog(null,
                        "An Error occurred when we tried to reschedule "
                                + e1.getWrongApp().getName());
                e1.printStackTrace();
            }
            ad.getCW().setAd(null);
            ad.dispose();
            dispose();
        }
    });
    btnReschedule.setBounds(289, 85, 89, 23);
    panel.add(btnReschedule);
}

}
